<!--/**
* Created by IntelliJ IDEA.
* User: Winnie
* Date: 07/04/14
* Time: 12:11
*/ -->
<?php
    session_start();	//start session
    if(!isset($_SESSION['username'])){	//check if the user data is in the session
        header("location: login.php");	//if not redirect back to user
    }
?>
<html>
    <head>
        <title>Users</title>
        <link rel="stylesheet" href="style.css">
        <script src="jquery-1.11.0.js"></script>
        <script src="gen.js"></script>
        <script>
            function syncAjax(u){
                var obj= $.ajax(
                    {url:u,
                     async:false
                    }
                );
                return $.parseJSON(obj.responseText);
            }
            function popupEditUser(obj,userId){
                var r=getUserById(userId);
                if(r.result == 0){
                    return;
                }
                $("#userId").prop("value", r.user.id);
                $("#firstName").prop("value", r.user.firstname);
                $("#lastName").prop("value", r.user.lastname);
                $("#userName").prop("value", r.user.username);
                $("#userType").prop("value", r.user.usertype);

                var y=event.clientY;
                var x=event.clientX/2;

                $("#editPopup").css("top",y);
                $("#editPopup").css("left",x);
                //display the form
                $("#editPopup").fadeIn(1000);

            }
            function saveUpdate(){

              var userID = document.getElementById("userId").value;
               var first_name = document.getElementById("firstName").value;
               var last_name = document.getElementById("lastName").value;
               var username = document.getElementById("userName").value;
               var user_type = document.getElementById("userType").value;

              var u = "userAction.php?cmd=3&id="+userID+"&fName="+first_name+"&lName="+last_name+"&uName="+username+"&uType="+user_type;

              var result = syncAjax(u);

                if(result.result=0){
                    cancel();
                    alert(result.message);
                    return;
                }
                else{
                    cancel();
                    alert(result.message);
                }
                location.reload();

            }

            function getUserById(userId){
               var u="userAction.php?cmd=1&id="+userId;
                return syncAjax(u);
            }

            function popupAddUser(){
				var y=event.clientY;
                var x=event.clientX/2;

                $("#addPopup").css("top",y);
                $("#addPopup").css("left",x);

                $("#addPopup").fadeIn(1000);
            }

            function saveNew(){
                var userID=document.getElementById("userId").value;
                var fName =document.getElementById("first_name").value;
                var lName =document.getElementById("last_name").value;
                var uName =document.getElementById("username").value;
                var pWord =document.getElementById("password").value;
                var uType =document.getElementById("user_type").value;

                var u = "userAction.php?cmd=4&id="+userID+"&fName="+fName+"&lName="+lName+"&uName="+uName+"&pword="+pWord+"&uType="+uType;
               // alert(u);
                var result = syncAjax(u);
				if(result.result=0){
                    cancel();
                    alert(result.message);
                    return;
                }
                cancel();
                alert(result.message);
                
                location.reload();
			}

            function cancel(){
                $("#editPopup").fadeOut(1000);
                $("#addPopup").fadeOut(1000);
            }


            function searchByUsername(){
                var u_name = document.getElementById("search").value;

                var url = "userAction.php?cmd=5&u_name="+u_name;

                $('.row1').remove();
                $('.row2').remove();

                var tab = document.getElementById("table");
                var allusers = syncAjax(url);

                if (allusers.result==0){
                    return;
                }else{
                    for(var i=0; i<allusers.user.length; i++){
                        console.log("logging"+i);
                        var rows = tab.insertRow(i+1);
                        var column1 = rows.insertCell(0);
                        var column2 = rows.insertCell(1);
                        var column3 = rows.insertCell(2);
                        var column4 = rows.insertCell(3);
                        var column5 = rows.insertCell(4);

                        if (i%2==0){
                            rows.className="row1";
                        }else{
                            rows.className="row2";
                        }
                        column1.innerHTML = allusers.user[i].firstname;
                        column2.innerHTML = allusers.user[i].lastname;
                        column3.innerHTML = allusers.user[i].username;
                        column4.innerHTML = allusers.user[i].usertype;
                        column5.innerHTML = "<a onclick='popupEditUser(this,allusers.user[i].id)'> EDIT </a> "
                    }
                }
            }

        </script>
    </head>
 <body>
    <table id="contentTable">
        <tr>
            <td colspan="2" id="pageheader">
                HEALTH INFORMATION SYSTEMS
            </td>
        </tr>
        <tr>
            <td id="mainnav">
                <div class="menuitem"><a href = "userslist.php">Users</a></div>
                <div class="menuitem" onclick="popupAddUser()">ADD A USER</div>
                <div class="menuitem"><a href="logout.php">LOGOUT</a></div>
                <div class="menuitem"></div>
           </td>
            <td id="content">
                <div id="divPageMenu">
                    <input type="text" placeholder="Search By Username" id="search">
                    <input type="button" value="Search" onclick="searchByUsername()">
                </div>
				


    <div id="divContent">
    <table class="reportTable" width="100%" id="table">
        <tr class="header">
            <th> First Name </th>
            <th> Last Name </th>
            <th> Username </th>
            <th> User Type </th>
            <th></th>
      <!--      <th> Date Created </th> -->
        </tr>
        <?php
        include("users.php");
        $obj = new users();

        if(!$obj-> get_all_users()){
            echo ("Error");//.mysql_error($obj->link);
            exit();
        }

        $row=$obj->fetch();
        $row_counter=0;
        while($row){
            if($row_counter%2==0){
                $style=" class='row1' ";
            }else{
                $style=" class='row2'  ";
            }
            $userId = $row['userId'];
            echo "<tr $style ><td id='f_name'>".$row['firstname']. "</td>";
            echo "<td >".$row['lastname']."</td>";
            echo "<td >".$row['username']."</td>";
            echo "<td >".$row['usertype']."</td>";
          //  echo "<td>".$row['datecreated']."</td>";
            echo "<td><span onclick='popupEditUser(this,$userId);'> EDIT </span></td></tr>";
            $row=$obj->fetch();
            $row_counter++;
        }

        ?>

                </table>
            </div>
        </td>
     </tr>
   </table>

        <div id="editPopup" class="popupForm">
            <table class="tableForm">
                <tr>
                    <td><input type="hidden" value="" id="userId"></td>
                </tr>
                <tr>
                    <td class="label">First name</td>
                    <td><input type="text" value="" id="firstName" class="field"></td>
                </tr>
                <tr>
                    <td class="label">Last name </td>
                    <td><input type="text" value="" id="lastName" class="field"></td>
                </tr>
                <tr>
                    <td class="label">Username</td>
                    <td><input type="text" value="" id="userName" class="field"></td>
                </tr>
                <tr>
                    <td class="label" >User Type</td>
                    <td >
                        <select id="userType" class="field">
                            <option>ADMIN</option>
                            <option>CHO</option>
                            <option>DHMT</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td><input type="submit" value="SAVE" onclick="saveUpdate()" ></td>
                    <td><input type="button" value="CANCEL" onclick="cancel()"></td>
                </tr>
            </table>
        </div>

        <!--Add popup form-->
        <div id="addPopup" class="popupForm">
            <table class="tableForm">
                <tr>
                    <td class="label">Firstname</td>
                    <td><input type="text" value="" id="first_name" class="field"> </td>
                </tr>
                <tr>
                    <td class="label">Lastname</td>
                    <td><input type="text" value="" id="last_name" class="field"></td>
                </tr>
                <tr>
                    <td class="label">Username</td>
                    <td><input type="text" value="" id="username" class="field"></td>
                </tr>
                <tr>
                    <td class="label">Password</td>
                    <td><input type="password" value="" id="password" class="field"></td>
                </tr>
                <tr>
                    <td class="label">User Type</td>
                    <td>
                        <select id="user_type" class="field">
                            <option>ADMIN</option>
                            <option>CHO</option>
                            <option>DHMT</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td><input type="hidden" id="userId"></td>
                </tr>
                <tr>
                    <td><input type="submit" value="SAVE" onclick="saveNew()" ></td>
                    <td><input type="button" value="CANCEL" onclick="cancel()"></td>
                </tr>
            </table>
        </div>

    </body>
</html>